package ar.com.ford.it.fullEconomics.view.form;

import java.util.Collection;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.util.MessageResources;
import org.apache.struts.validator.ValidatorForm;
import ar.com.ford.it.fullEconomics.persistence.dto.Teess01Supplier;

@SuppressWarnings("serial")
public class SupplierForm extends ValidatorForm {
    String codeFind = null;
    String descFind = null;
    String operation = null;
    String section = null;
    String numberEconomicValue = null;
    String validateSubmit = null;
    Teess01Supplier supplierSelected = null;
    String supplierCodeN = null;
    String supplierClassN = null;
    String supplierNameN = null;
    Collection classCode = null;
    Collection suppliers = null;
    String numberSupplier = null;
    String supplierClassCode = null;

    /**
     * @return the supplierClassCode
     */
    public String getSupplierClassCode() {
        return supplierClassCode;
    }

    /**
     * @param supplierClassCode the supplierClassCode to set
     */
    public void setSupplierClassCode(String supplierClassCode) {
        this.supplierClassCode = supplierClassCode;
    }

    public String getNumberSupplier() {
        return numberSupplier;
    }

    public void setNumberSupplier(String numberSupplier) {
        this.numberSupplier = numberSupplier;
    }

    public Collection getSuppliers() {
        return suppliers;
    }

    public void setSuppliers(Collection supplier) {
        this.suppliers = supplier;
    }

    public String getCodeFind() {
        return codeFind;
    }

    public void setCodeFind(String codeFind) {
        this.codeFind = codeFind;
    }

    public String getDescFind() {
        return descFind;
    }

    public void setDescFind(String descFind) {
        this.descFind = descFind;
    }

    public String getNumberEconomicValue() {
        return numberEconomicValue;
    }

    public void setNumberEconomicValue(String numberEconomicValue) {
        this.numberEconomicValue = numberEconomicValue;
    }

    public String getOperation() {
        return operation;
    }

    public void setOperation(String operation) {
        this.operation = operation;
    }

    public String getSection() {
        return section;
    }

    public void setSection(String section) {
        this.section = section;
    }

    public String getSupplierClassN() {
        return supplierClassN;
    }

    public void setSupplierClassN(String supplierClassN) {
        this.supplierClassN = supplierClassN;
    }

    public String getSupplierCodeN() {
        return supplierCodeN;
    }

    public void setSupplierCodeN(String supplierCodeN) {
        this.supplierCodeN = supplierCodeN;
    }

    public String getSupplierNameN() {
        return supplierNameN;
    }

    public void setSupplierNameN(String supplierNameN) {
        this.supplierNameN = supplierNameN;
    }

    public Teess01Supplier getSupplierSelected() {
        return supplierSelected;
    }

    public void setSupplierSelected(Teess01Supplier supplierSelected) {
        this.supplierSelected = supplierSelected;
    }

    public String getValidateSubmit() {
        return validateSubmit;
    }

    public void setValidateSubmit(String validateSubmit) {
        this.validateSubmit = validateSubmit;
    }

    public void SupplierResetPanel() {
        this.codeFind = null;
        this.descFind = null;
        this.operation = null;
        this.section = null;
        this.numberEconomicValue = null;
        this.validateSubmit = null;
        this.supplierSelected = null;
        this.supplierCodeN = null;
        this.supplierClassN = null;
        this.supplierNameN = null;

    }

    public void SupplierReset() {
        this.supplierCodeN = null;
        this.supplierClassN = null;
        this.supplierNameN = null;
        this.validateSubmit = null;
    }

    public void setEditMapped(String key, String value) {
        List suppliers = (List) this.getSuppliers();
        Teess01Supplier supS = (Teess01Supplier) suppliers.get(Integer
                .parseInt(key) - 1);
        this.setSupplierSelected(supS);
    }

    public Collection getClassCode() {
        return classCode;
    }

    public void setSeleccMapped(String key, String value) {

    }
    public boolean getSeleccMapped(String key){
        return false;
    }
    
    public void setClassCode(Collection classCode) {
        this.classCode = classCode;
    }

    public ActionErrors validate(ActionMapping mapping,
                                 HttpServletRequest request) {
        ActionErrors ae = new ActionErrors();
        MessageResources resources = (MessageResources) request
                .getAttribute(org.apache.struts.Globals.MESSAGES_KEY);
        if ("find".equals(this.section)) {
            if ((StringUtils.isEmpty(this.getCodeFind()))
                    && (StringUtils.isEmpty(this.getDescFind()))) {
            } else {
                if (this.getCodeFind().length()>5) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE,
                            new ActionMessage("supplier.error.lenghtCode"));
                }
            }
            if(this.getDescFind().length()>70){
                ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                "supplier.error.lenghtName"));
            }
        }
        if ("validar".equals(this.getValidateSubmit())) {

                if ("create".equals(this.operation)) {
                    if ("-1".equals(this.getSupplierClassN())) {
                        ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                                "supplier.error.selecProvee"));
                    }
                    
                    if (StringUtils.isEmpty(this.getSupplierCodeN())) {
                        ae.add(ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage("supplier.error.code"));
                    }else{
                        if (this.getSupplierCodeN().length()>5) {
                            ae.add(ActionMessages.GLOBAL_MESSAGE,
                                    new ActionMessage("supplier.error.lenghtCode"));
                        }
                    }
                    if (StringUtils.isEmpty(this.getSupplierNameN())) {
                        ae.add(ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage("supplier.error.noName"));
                    }else{
                        if(this.getSupplierNameN().length()>70){
                            ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "supplier.error.lenghtName"));
                        }
                    }
                }
                if ("edit".equals(this.section)) {
                    if (StringUtils.isEmpty(this.getSupplierSelected()
                            .getEess01SupplierNameN())) {
                        ae.add(ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage("supplier.error.noName"));
                    }
                    if(this.getSupplierSelected()
                            .getEess01SupplierNameN().length()>70){
                        ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                        "supplier.error.lenghtName"));
                    }
                }
            }
        
        return ae;
    }
}
